package com.example.androidlearning.sy12.sy2musicplayer;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.androidlearning.R;

public class MusicPlayerActivity extends AppCompatActivity {

    private Button start;
    private Button stop;
    private Intent intent;
    private SeekBar seekBar;
    private ServiceConnection serviceConnection;
    private MusicService.MusicBinder binder;
    private Handler handler;
    private Runnable runnable;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sy1202_main);

        init();
        setListener();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnable);
        stopService(intent);
        binder.closeMedia();
        unbindService(serviceConnection);
    }

    void init(){

        start = findViewById(R.id.sy12_play);
        stop = findViewById(R.id.sy12_stop);
        seekBar = findViewById(R.id.sy12_seekbar);

        intent = new Intent("android.media");
        intent.setPackage(getContext().getPackageName());

        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                binder = (MusicService.MusicBinder) service;
                seekBar.setMax(binder.getProgress());

                seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                        //这里很重要，如果不判断是否来自用户操作进度条，会不断执行下面语句块里面的逻辑，然后就会卡顿卡顿
                        if(fromUser){
                            binder.seekToPosition(seekBar.getProgress());
                        }
                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {

                    }

                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {

                    }
                });

                handler.post(runnable);

            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };

        startService(intent);
        bindService(intent, serviceConnection, BIND_AUTO_CREATE);
        handler = new Handler();

         runnable = new Runnable() {
            @Override
            public void run() {
                seekBar.setProgress(binder.getPlayPosition());
                handler.postDelayed(runnable, 1000);
            }
         };
    }

    void setListener(){

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binder.playMusic();
            }
        });
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binder.pauseMusic();
            }
        });

    }



    private Context getContext(){
        return this;
    }
}
